package interceptor;

import entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/7/29 15:01
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class CheckLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if(null == user){
            request.setAttribute("loginMsg","请先登录");
            request.getRequestDispatcher("/showLogin").forward(request,response);
            return false;
        }
        return true;
    }
}
